package com.briup.service.impl;

import com.briup.exception.ServiceException;
import com.briup.mapper.UserMapper;
import com.briup.pojo.User;
import com.briup.pojo.dto.UserBaseDto;
import com.briup.pojo.dto.UserLogin;
import com.briup.pojo.dto.UserPageDto;
import com.briup.pojo.vo.UserPageVO;
import com.briup.response.ResultCode;
import com.briup.service.UserService;
import com.briup.utils.BeanCopyUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;


@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    /**
     * 用户登录逻辑处理
     *
     * @param userLogin 用户登录对象 内含用户名及密码
     *                  后可添加验证码等数据
     * @return user
     */

    @Override
    public User login(UserLogin userLogin) {
        // 判断用户名和密码是否存在值(参数校验)
        if (userLogin == null
                || !StringUtils.hasText(userLogin.getUsername())
                || !StringUtils.hasText(userLogin.getPassword())) {
            throw new ServiceException(ResultCode.PARAM_NOT_COMPLETE);
        }

        User user = userMapper.getUserByUsername(userLogin.getUsername());

        // 判断用户是否存在及密码是否正确
        if (user == null || !userLogin.getPassword().equals(user.getPassword())) {
            throw new ServiceException(ResultCode.USER_LOGIN_ERROR);
        }

        // 判断用户状态是否可用 用户状态(0正常，1禁用)
        if (user.getStatus() == 1) {
            throw new ServiceException(ResultCode.USER_ACCOUNT_FORBIDDEN);
        }
        return user;
    }

    /**
     * 分页多条件查询用户信息
     *
     * @param userPageDto 用于封装分页多条件查询用户时的条件参数
     */
    @Override
    public PageInfo<UserPageVO> getUsersPageByConditional(UserPageDto userPageDto) {
        // 使用PageHelper开启分页,设置分页参数
        PageHelper.startPage(userPageDto.getPageNum(), userPageDto.getPageSize());

        // 根据条件查询所有相关的用户
        List<User> userList = userMapper.getUserListByConditional(userPageDto);
        Page<User> page = (Page<User>) userList;

        // Bean拷贝
        List<UserPageVO> userPageVOS = BeanCopyUtils.copyBeanList(userList, UserPageVO.class);

        // 封装在PageInfo对象中,并返回
        PageInfo<UserPageVO> pageInfo = new PageInfo<>(userPageVOS);
        // 设置总条数
        pageInfo.setTotal(page.getTotal());

        return pageInfo;
    }

    @Override
    public void deleteUsers(List<Integer> ids) {
        userMapper.deleteUsers(ids);
    }

    @Override
    public void addUser(UserBaseDto userBaseDto) {
        if (!StringUtils.hasText(userBaseDto.getUsername()) ||
                !StringUtils.hasText(userBaseDto.getPassword()) ||
                userBaseDto.getStatus() == null) {
            throw new ServiceException(ResultCode.PARAM_NOT_COMPLETE);
        }
        // 判断用户是否存在
        User userByUsername = userMapper.getUserByUsername(userBaseDto.getUsername());
        if (userByUsername != null) {
            throw new ServiceException(ResultCode.USER_HAS_EXISTED);
        }
        // 3.如果用户未被注册,则将参数实体内容拷贝到用户实体中，并补全信息
        User user = BeanCopyUtils.copyBean(userBaseDto, User.class);
        user.setRegisterTime(LocalDateTime.now());
        user.setRoleId((short) 2);
        user.setIsVip(false);
        if (user.getGender() == null) {
            user.setGender("0");
        }
        log.info("用户{}", user);
        userMapper.insertUser(user);
    }

    @Override
    public User getUserById(Integer id) {
        User user = userMapper.getUserById(id);
        user.setPassword(null);
        return user;
    }

    @Override
    public void updateUser(UserBaseDto userBaseDto) {
        if (userBaseDto.getId() == null) {
            throw new ServiceException(ResultCode.DATA_NONE);
        }
        User user = userMapper.getUserById(userBaseDto.getId());
        if (user == null) {
            throw new ServiceException(ResultCode.DATA_NONE);
        }

        if (userBaseDto.getUsername() != null && !userBaseDto.getUsername().equals(user.getUsername())){
            User newUser = userMapper.getUserByUsername(userBaseDto.getUsername());
            if (newUser != null) {
                throw new ServiceException(ResultCode.USER_HAS_EXISTED);
            }
        }
        userMapper.updateUser(userBaseDto);
    }
}
